package test;

import common.BaseController;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.BusinessMajorServices;
import services.dictionary.MajorTypeServices;
import util.JsonUtil;
import wt.entity.business.BusinessInfo;
import wt.entity.business.BusinessMajor;
import wt.entity.dictionary.MajorType;
import wt.staticData.ShortData;
import wt.webPlugin.ZTree;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping("httpPage")
public class PageController extends BaseController {



    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    BusinessMajorServices businessMajorServices;


    @RequestMapping(value = "addBusinessPage")
    public String addBusinessPage(HttpServletRequest request) {
        return "admin/business/addBusiness";
    }

    @ResponseBody
    @RequestMapping(value = "addBusiness", produces = "application/json;charset=UTF-8")
    public Object addBusiness(String businessCode, String companyName, String companyCode, String address,
                              String productName, String businessType, String applicationTime, String acceptTime,
                              String itemNum, String managerUserId, String observerUserId, String majorNumList) throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("companyName", companyName);
        data.put("companyCode", companyCode);
        data.put("address", address);
        data.put("productName", productName);
        data.put("businessType", businessType);
        data.put("applicationTime", applicationTime+" 00:00:00");
        data.put("acceptTime", acceptTime+" 00:00:00");
        data.put("itemNum", itemNum);
        data.put("majorNumList", majorNumList);
        data.put("managerUserId", managerUserId);
        data.put("observerUserId", observerUserId);
        return Properties.sendData(Properties.addBusinessInfo, data, Properties.uid, Properties.key);

    }


    @RequestMapping(value = "extractExpertPage")
    public String extractExpertPage(String businessCode, Map map) throws Exception {
        if (StringUtils.isBlank(businessCode)) {
            map.put("errorMessage","businessCode 为空");
            return "admin/common/errorPage";
        }

        BusinessInfo businessInfo = businessInfoServices.selectByBusinessCode(businessCode);
        map.put("business", businessInfo);
        return "admin/business/extractExpert/index";
    }


    @ResponseBody
    @RequestMapping(value = "automaticExtractionExpert", produces = "application/json;charset=UTF-8")
    public Object automaticExtractionExpert(String businessCode, Integer count,String affiliateCompany, String startTime, String endTime, String leaderGradeList) throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("count", count);
        data.put("leaderGradeList", leaderGradeList);
        data.put("affiliateCompany", affiliateCompany);
        data.put("startTime", startTime);
        data.put("endTime", endTime);
        String resultMessage = Properties.sendData(Properties.automaticExtractionExpert, data, Properties.uid, Properties.key);
        return ((Map) JsonUtil.json2Obj(resultMessage, Map.class)).get("data");

    }

    @ResponseBody
    @RequestMapping(value = "manuallyExtractionExpert", produces = "application/json;charset=UTF-8")
    public Object manuallyExtractionExpert(String name, String outLander, String businessCode,String affiliateCompany, Integer offset, Integer limit, String startTime, String endTime, String gradeList, String majorNumList) throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("startTime", startTime);
        data.put("endTime", endTime);
        data.put("majorNumList", majorNumList);
        data.put("affiliateCompany", affiliateCompany);
        data.put("gradeList", gradeList);
        data.put("name", name);
        data.put("outLander", outLander);
        data.put("offset", offset);
        data.put("limit", limit);
        return ((Map) JsonUtil.json2Obj(Properties.sendData(Properties.manuallyExtractionExpert, data, Properties.uid, Properties.key), Map.class)).get("data");
    }


    @ResponseBody
    @RequestMapping(value = "addBusinessExperts", produces = "application/json;charset=UTF-8")
    public Object addBusinessExperts(String businessCode, String startTime, String endTime, String leaderIdCard, String crewIdCardList) throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("leaderIdCard", leaderIdCard);
        data.put("crewIdCardList", crewIdCardList);
        data.put("startTime", startTime);
        data.put("endTime", endTime);
        return Properties.sendData(Properties.addBusinessExperts, data, Properties.uid, Properties.key);
    }

    @RequestMapping(value = "addBusinessResultPage")
    public String addBusinessResultPage() {
        return "admin/business/addBusinessResult";
    }

    @ResponseBody
    @RequestMapping(value = "addBusinessResult", produces = "application/json;charset=UTF-8")
    public Object addBusinessResult(String businessCode, String startTime, String endTime, Integer result) throws Exception {

        Map<String, Object> data = new HashMap();
        data.put("businessCode", businessCode);
        data.put("result", result);
        data.put("startTime", startTime);
        data.put("endTime", endTime);
        return JsonUtil.json2Obj(Properties.sendData(Properties.addBusinessResult, data, Properties.uid, Properties.key), Map.class);

    }


    /**
     * 获取专业树
     *
     * @param
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "getProfessionTree", produces = "application/json;charset=UTF-8")
    public Object getProfessionSetTree() throws Exception {
        List<MajorType> majorTypeList = majorTypeServices.selectMajorType(null, true);
        JSONArray jsonArray = new JSONArray();
        for (MajorType majorType : majorTypeList) {
            JSONObject jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName(), majorType.getNum());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }


    @ResponseBody
    @RequestMapping(value = "getTreeByBusinessId", produces = "application/json;charset=UTF-8")
    public Object getTreeALLByBusinessPk(String  businessId) throws Exception {

        BusinessInfo businessInfo = businessInfoServices.selectByPrimaryKey(businessId);
        List<MajorType> majorTypeList = majorTypeServices.selectMajorType(businessInfo.getItemId(), true);
        List<BusinessMajor> businessMajorList = businessMajorServices.selectByBusinessId(businessInfo.getId());
        JSONArray jsonArray=new JSONArray();

        for (MajorType majorType : majorTypeList) {
            majorType.setLive(ShortData.s0);
            for (BusinessMajor businessMajor : businessMajorList) {
                if (majorType.getNum().equals(businessMajor.getMajorNum())) {
                    majorType.setLive(ShortData.s1);
                    break;
                }
            }
            if (majorType.getLive() == 1 ) {
                jsonArray.add(ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName(), true,majorType.getNum()));
            } else {
                jsonArray.add(ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName(),false,majorType.getNum()));
            }
        }
        return jsonArray;

    }





}
